package com.medical.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.config.SystemContext;
import com.medical.dao.SysMbflMapper;
import com.medical.entity.SysMbfl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;


@Service
@Transactional
public class MbflService {

    @Autowired
    private SysMbflMapper sysMbflMapper;


    /**
     * 根据用户ID查找用户信息
     * @param mbflId
     * @return
     */
    public SysMbfl findMbflByMbflId(Integer mbflId) {
        return sysMbflMapper.findMbflByMbflId(mbflId);
    }

    /**
     * 查找用户列表
     * @return
     */
    public PageInfo<SysMbfl> getMbflList(String mbflName, Integer currentPage, Integer limit){
        //利用PageHelper分页查询 注意：这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
        PageHelper.startPage(currentPage, limit);
        List<SysMbfl> mbflList = sysMbflMapper.getMbflList(mbflName);
        PageInfo<SysMbfl> pageInfo = new PageInfo<SysMbfl>(mbflList);
        return pageInfo;
    }

    /**
     * 根据ID获取用户信息
     * @param id
     * @return
     */
    public SysMbfl getMbflById(Integer id) {
        return sysMbflMapper.selectByPrimaryKey(id);
    }

    /**
     * 添加
     * @param sysMbfl
     */
    public void addMbfl(SysMbfl sysMbfl) {
        sysMbfl.setCreateTime(new Timestamp(System.currentTimeMillis()));
        sysMbfl.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        sysMbfl.setmbflOther(SystemContext.passwdEncryption(sysMbfl.getmbflOther()));

        sysMbflMapper.insert(sysMbfl);
    }

    /**
     * 更新
     * @param sysMbfl
     */
    public void edit(SysMbfl sysMbfl) {
        sysMbfl.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        sysMbflMapper.updateByPrimaryKeySelective(sysMbfl);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(Integer id) {
        sysMbflMapper.deleteByPrimaryKey(id);
    }
}
